import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router';
import { usePermissStore } from '../store/permiss';
import Home from '../views/home.vue';

const routes: RouteRecordRaw[] = [
    {
        path: '/',
        redirect: '/dashboard',
    },
    {
        path: '/',
        name: 'Home',
        component: Home,
        children: [
            {
                path: '/dashboard',
                name: 'dashboard',
                meta: {
                    title: '系统首页',
                    permiss: '1',
                },
                component: () => import('../views/dashboard.vue'),
            },

            {
                path: '/list_user',
                name: 'list_user',
                meta: {
                    title: '用户列表',
                    permiss: '11',
                },
                component: () => import('../pages/list_user.vue'),
            },

            {
                path: '/parking_record',
                name: 'parking_record',
                meta: {
                    title: '停车记录',
                    permiss: '11',
                },
                component: () => import('../pages/parking_record.vue'),
            },

            {
                path: '/vehicle',
                name: 'vehicle',
                meta: {
                    title: '车辆管理',
                    permiss: '11',
                },
                component: () => import('../pages/vehicle.vue'),
            },
            {
                path: '/fee_record',
                name: 'fee_record',
                meta: {
                    title: '订单费用记录',
                    permiss: '11',
                },
                component: () => import('../pages/fee_record.vue'),
            },
            {
                path: '/park_lot',
                name: 'park_lot',
                meta: {
                    title: '停车场信息',
                    permiss: '11',
                },
                component: () => import('../pages/parking_lot.vue'),
            },
            
            {
                path: '/export_user',
                name: 'export_user',
                meta: {
                    title: '导出用户信息',
                    permiss: '11',
                },
                component: () => import('../pages/export_user.vue'),
            },
            {
                path: '/user',
                name: 'user',
                meta: {
                    title: '个人中心',
                },
                component: () => import('../views/user.vue'),
            },
           
        ],
    },
    {
        path: '/login',
        name: 'Login',
        meta: {
            title: '登录',
        },
        component: () => import('../views/login.vue'),
    },
    {
        path: '/403',
        name: '403',
        meta: {
            title: '没有权限',
        },
        component: () => import('../views/403.vue'),
    },
];

const router = createRouter({
    history: createWebHashHistory(),
    routes,
});

// 路由守卫，判断用户是否登陆
router.beforeEach((to, from, next) => {
    document.title = `${to.meta.title} | 中电长城停车场`;
    const role = localStorage.getItem('administrator_role');
    const permiss = usePermissStore();
    if (!role && to.path !== '/login') {
        next('/login');
    } else if (to.meta.permiss && !permiss.key.includes(to.meta.permiss)) {
        // 如果没有权限，则进入403
        next('/403');
    } else {
        next();
    }
});

export default router;
